//定义全局变量
var $parent = $(".shopcart-item-box"); //购物车栏目区域
var $selectAll = $('.select-all');
var totalLast; //当前合计金额
var $curTotalEle = $(".total-price");
var unitItemNum = 0;

var identifier = true; //identifier是点击数量增减/是否勾选商品的标识

//当前选中商品单价和数量
function getMerInfo(ele) {
	var unitPrice = ele.siblings().find('.unit-price').text();
	var unitNum = ele.siblings().find('.cart-unit-num').val();
	var infos = {
		unitPrice: unitPrice,
		unitNum: unitNum
	};
	return infos;
}

//价格加
function plusMod(ele, totalLast, identifier, unitItemNum) {
	var infos = getMerInfo(ele);
	var totalAcount = 0;
	console.log(unitItemNum);
	if(identifier) {
		totalAcount = (+totalLast) + (infos.unitPrice * infos.unitNum);
	} else {
		totalAcount = (+totalLast) + (+infos.unitPrice) + (unitItemNum - 1);
	}

	$curTotalEle.text(totalAcount.toFixed(2));

	var selectedItem = ele.parents('.shopcart-item-box').children().find('.merselect.selected');
	var itemsLen = ele.parents('.shopcart-item-box').children().length;
	console.log(selectedItem.length + ":" + itemsLen);
	(selectedItem.length == itemsLen) && ($selectAll.addClass('selected'));

}
//价格减
function reduceMod(ele, totalLast, identifier) { //identifier是点击数量增减/是否勾选商品的标识
	var infos = getMerInfo(ele);
	var totalAcount = 0; //总额
	if(identifier) {
		totalAcount = (+totalLast) - (infos.unitPrice * infos.unitNum);
	} else {
		totalAcount = (+totalLast) - (infos.unitPrice);
	}

	console.log('商品单价：' + infos.unitPrice + '商品数量：' + infos.unitNum);
	console.log('上次合计金额为：' + totalLast + "当前减去商品后的金额：" + totalAcount);

	$curTotalEle.text(totalAcount.toFixed(2));
}

function computeAll() {
	var $allUnitPrice = $('.shopcart-item .unit-price');
	var $allItemNum = $('.shopcart-item .cart-unit-num');
	var totalPrice = 0;
	for(var i = 0, len = $allUnitPrice.length; i < len; i++) {
		var allPriceVal = $allUnitPrice.eq(i).text();
		var allNumVal = $allItemNum.eq(i).val();
		totalPrice += allPriceVal * allNumVal;
		$curTotalEle.text(totalPrice.toFixed(2));
	}

}

$(function() {

	//点击选择商品
	$parent.on('click', '.merselect', function() {
		var $that = $(this);
		totalLast = $curTotalEle.text(); //当前总价

		if($that.hasClass('selected')) { //已选中该商品
			$that.removeClass('selected');
			$selectAll.removeClass('selected');

			reduceMod($that, totalLast, identifier);
			//去掉全选选中状态
			$selectAll.removeClass('selected');

		} else {
			$that.addClass('selected');
			plusMod($that, totalLast, identifier);

		}

	});
	//点击减少商品数量
	$parent.on('click', '.cart-reduce', function() {
		var $that = $(this);
		var curItemEle = $that.siblings('.cart-unit-num'),
			curItemNum = curItemEle.val();
		totalLast = $curTotalEle.text();
		unitItemNum <= 0 ? unitItemNum = 0 : (unitItemNum--)
		if(curItemNum <= 1) {
			curItemEle.val('1');
		} else {
			curItemNum--;
			curItemEle.val(curItemNum);
			var $ele = $that.parent().siblings('.merselect');
			if($ele.hasClass('selected')) {
				var identifier = false;
				reduceMod($ele, totalLast, identifier);
			}
		}
	});
	//点击增加商品数量
	$parent.on('click', '.cart-add', function() {
		var $that = $(this);
		var curItemEle = $that.siblings('.cart-unit-num'),
			curItemNum = curItemEle.val();
		totalLast = $curTotalEle.text();
		curItemNum++;
		curItemEle.val(curItemNum);

		unitItemNum++;
		if($that.parent().siblings('.merselect').hasClass('selected')) {
			var identifier = false;
			var $ele = $that.parent().siblings('.merselect');
			plusMod($ele, totalLast, identifier, unitItemNum);
			unitItemNum = 0;
		}

	});

	//全选事件处理
	var $itemsSelect = $('.shopcart-item-box').children().find('.merselect');
	var selet = true;
	$selectAll.click(function() {
		var $that = $(this);
		if(selet) {
			$that.addClass('selected');
			$itemsSelect.addClass('selected');
			computeAll();
			selet = false;
		} else {
			selet = true;
			$that.removeClass('selected');
			$itemsSelect.removeClass('selected');
			$curTotalEle.text('0.00');
		}
	});

	//点击编辑事件处理
	var $editBtn = $('.edit-btn'),
		$total = $('.total-area'),
		$settleBtn = $('.settle-btn');
	$delBtn = $('.del-btn');
	var editSwitch = true;
	$editBtn.click(function() {
		if(editSwitch) {
			$(this).text('完成');
			$total.addClass('hide');
			$settleBtn.addClass('hide');
			$delBtn.removeClass('hide');
			editSwitch = false;
		} else {
			$(this).text('编辑');
			$total.removeClass('hide');
			$settleBtn.removeClass('hide');
			$delBtn.addClass('hide');
			$selectAll.removeClass('selected');
			$itemsSelect.removeClass('selected');
			$curTotalEle.text('0.00');
			selet = true;
			editSwitch = true;
		}
	});

	//删除商品
	$delBtn.click(function() {
		var $ele = $(".shopcart-item-box .shopcart-item .selected");
		console.log($ele.length);
		if($ele.length == 0) {
			YDUI.dialog.alert('请至少选择一项商品！');
		} else {
			YDUI.dialog.confirm('删除商品提示', '请确认是否删除该商品！', function() {
				cartOperate($ele,true);
			});
		}
	});

	//结算商品
	$settleBtn.click(function() {
		var $selectitem = $('.shopcart-item .merselect.selected');
		if($selectitem.length == 0) {
			YDUI.dialog.alert('请至少选择一项商品！');
		} else {
			cartOperate($selectitem,false);
		}

	});

	function cartOperate($ele,delJudge) { //结算或删除操作
		if(delJudge){
			//删除操作
			$.ajax({
				type: "post",
				url: "",
				success: function() {
	
				}
			});
			$ele.parent('.shopcart-item').remove();
			$selectAll.removeClass('selected');
		}else{
			//结算
			YDUI.dialog.loading.open('正在为您结算');
			$.ajax({
				type:'post',
				url: "",
				success: function() {
	
				},
				complete:function(){
					//YDUI.dialog.loading.close();
				}
			})
		}
		
		
	}

})